package com.zwjstudy.ehcache2;

import com.zwjstudy.ehcache2.entity.Dog;
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

/**
 * ClassName: Ehcache2Test2
 * Package: com.zwjstudy.ehcache2
 * Description:
 *
 * @Author: 张文俊
 * @Create: 2025-09-17 - 10:56
 * @Version: v1.0
 */
public class Ehcache2Test3 {

    public static void main(String[] args) {
        //1.创建CacheManager
//        CacheManager cacheManager = CacheManager.create("./src/main/resources/ehcache.xml");
        //默认就是在类路径的ehcache.xml
//        CacheManager cacheManager=CacheManager.create();
        CacheManager cacheManager = CacheManager.newInstance();
        //2.获取cache实例
        Cache helloWorldCache = cacheManager.getCache("HelloWorldCache");

        //3.存入数据
        Dog dog1 = new Dog("小黑1","黑色",2);
        Dog dog2 = new Dog("小黑2","黑色",2);
        Element element1 = new Element("dog1",dog1);
        Element element2 = new Element("dog2",dog2);
        helloWorldCache.put(element1);
        helloWorldCache.put(element2);

        //4.获取缓存数据

        Element value = helloWorldCache.get("dog1");
        Dog objectValue = (Dog)value.getObjectValue();
        System.out.println("value="+value);
        System.out.println("objectValue = " + objectValue);

        System.out.println("删除前：");
        System.out.println("helloWorldCache.getSize() = " + helloWorldCache.getSize());
        helloWorldCache.getKeys().forEach(str -> System.out.println(str));

        helloWorldCache.remove("dog2");
        System.out.println("删除后：");
        System.out.println("helloWorldCache.getSize() = " + helloWorldCache.getSize());
        helloWorldCache.getKeys().forEach(str -> System.out.println(str));

        //5.关闭CacheManager
        cacheManager.shutdown();
    }
}
